﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Principal;
using System.IO;
using System.Web.Configuration;
using Core;

namespace WebTest
{
    public partial class IdentityTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //GetIdentityInfo();
            //WindowsIdentity userIdentity = (WindowsIdentity)User.Identity;
            //WindowsPrincipal userPrincipal = new WindowsPrincipal(userIdentity);
            //WindowsImpersonationContext ctx = null;
            //Response.Write("模拟请求用户开始<br/>");
            //ctx = userIdentity.Impersonate();
            //GetIdentityInfo();
            //Response.Write("模拟特定用户开始<br/>");
            if (!Page.IsPostBack)
            {
                using (IdentityScope c = new IdentityScope("diago", "192.168.0.224", "asdf123!"))
                {
                    GetIdentityInfo();
                    string[] filesName = Directory.GetFiles("\\\\192.168.0.224\\CommonLib");
                    Response.Write("长度=" + filesName.Length + "<br/>");
                }
            }
            
        //    Response.Write("模拟结束<br/>");
        //    if (ctx != null)
        //        ctx.Undo();
        //    GetIdentityInfo();
        }

        private void GetIdentityInfo()
        {
            WindowsIdentity userIdentity = WindowsIdentity.GetCurrent();
            WindowsPrincipal userPrincipal = new WindowsPrincipal(userIdentity);
            Response.Write(string.Format("系统用户标识：{0}<br/>", userPrincipal.Identity.Name));
            Response.Write(string.Format("身份验证类型：{0}<br/>", userPrincipal.
            Identity.AuthenticationType));
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write("111111111111111");
        }

    }
}
